﻿using System.Collections.Generic;
using System.Linq;

namespace Algoritmia.DataStructures.Maps
{
    public class ItemsView<TKey, TValue> : ICollectionView<KeyValuePair<TKey, TValue>>
    {
        protected IMap<TKey, TValue> _self;

        public ItemsView(IMap<TKey, TValue> self)
        {
            _self = self;
        }

        #region IEnumerable<KeyValuePair<TKey,TValue>> Members

        public virtual IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            return _self.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        #region ICountable Members

        public int Count
        {
            get { return _self.Count; }
        }

        #endregion
    }

    public class KeysView<TKey, TValue> : ItemsView<TKey, TValue>, ICollectionView<TKey>
    {

        public KeysView(IMap<TKey, TValue> self)
            : base(self)
        {
        }
        #region IEnumerable Members


        public new IEnumerator<TKey> GetEnumerator()
        {
            return _self.Select(kv => kv.Key).GetEnumerator();
        }

        #endregion

    }

    public class ValuesView<TKey, TValue> : ItemsView<TKey, TValue>, ICollectionView<TValue>
    {
        public ValuesView(IMap<TKey, TValue> self)
            : base(self)
        {
        }

        #region IEnumerable Members
        public new IEnumerator<TValue> GetEnumerator()
        {
            return _self.Select(kv => kv.Value).GetEnumerator();
        }
        #endregion

    }

}
